#include"bits/stdc++.h"
using namespace std;
const int MAXN=200000+10;
struct gamer{
	string name;
	char camp;
	long long proficiency_level;
};
int n,m,g_1=1,g_2=1;
string na;
char ca;
long long pl;
gamer survivors[MAXN],guardian[MAXN];
bool cmp(gamer x,gamer y)
{
	return x.proficiency_level>y.proficiency_level;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n+m;i++)
	{
		cin>>na>>ca>>pl;
		if(ca=='S')
		{
			survivors[g_1].name=na;
			survivors[g_1].camp=ca;
			survivors[g_1].proficiency_level=pl;
			g_1+=1;
		}
		else
		{
			guardian[g_2].name=na;
			guardian[g_2].camp=ca;
			guardian[g_2].proficiency_level=pl;
			g_2+=1;
		}
	}
	sort(survivors+1,survivors+1+n,cmp);
	sort(guardian+1,guardian+1+m,cmp);
	cout<<guardian[3].name<<endl;
	for(int i=6;i<=9;i++)
	{
		cout<<survivors[i].name<<endl;
	}
}
